﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;

namespace SEP.Admin
{
    public partial class ServiceReportViewDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if(Request.QueryString["id"] != null)
                {
                    Guid id;
                    string temp = Request.QueryString["id"].ToString();

                    if(Guid.TryParse(temp, out id))
                    {
                        BindServiceReport(id);
                    }
                }
            }
        }

        private void BindServiceReport(Guid id)
        {
            ServiceReport sr = null;
            
            try
            {
                sr = ServiceReport.GetByID(id);
                sr.ID = id;
            }
            catch { }

            if(sr.Instrument.Exists || !string.IsNullOrWhiteSpace(sr.PONumber))
            {
                pnlInstrument.Visible = true;
                litManufacturer.Text = sr.Instrument.ManufacturerType.Description;
                litInstrumentType.Text = sr.Instrument.InstrumentType.Description;
                litModel.Text = sr.Instrument.Model;
                litUnitNumber.Text = sr.Instrument.UnitNumber;
                litSerialNumber.Text = sr.Instrument.SerialNumber;
                litPONumber.Text = sr.PONumber;
            }
            else
            {
                pnlInstrument.Visible = false;
            }

            bool showServices = false;

            if(!string.IsNullOrWhiteSpace(sr.ServicePerformed))
            {
                pnlServicePerformed.Visible = true;
                showServices = true;
                litServicePerformed.Text = sr.ServicePerformed;
            }
            else
            {
                pnlServicePerformed.Visible = false;
            }

            if(!string.IsNullOrWhiteSpace(sr.ServiceRequired))
            {
                pnlServiceRequired.Visible = true;
                showServices = true;
                litServiceRequired.Text = sr.ServiceRequired;
            }
            else
            {
                pnlServiceRequired.Visible = false;
            }

            if(showServices)
            {
                pnlService.Visible = true;
            }
            else
            {
                pnlService.Visible = false;
            }
                        
            if(sr.Parts.Count > 0)
            {
                pnlParts.Visible = true;
                grvParts.DataSource = sr.Parts;
                grvParts.DataBind();
            }
            else
            {
                pnlParts.Visible = false;
            }

            if(sr.Totals.Count > 0)
            {
                grvTotals.DataSource = sr.Totals;
                grvTotals.DataBind();
            }

            this.UpdateTotals();
        }

        /// <summary>
        /// Updates all totals on the page.
        /// Calls UpdateGrandTotal() at the end of updating all previous totals.
        /// </summary>
        private void UpdateTotals()
        {
            decimal labor = 0.00M;
            decimal travel = 0.00M;
            decimal mileage = 0.00M;
            decimal other = 0.00M;
            decimal parts = 0.00M;

            foreach (GridViewRow row in grvTotals.Rows)
            {
                if (row.FindControl("litLabor") != null)
                {
                    labor += GetDecimalFromLiteral((Literal)row.FindControl("litLabor"));
                }
                if (row.FindControl("litTravel") != null)
                {
                    travel += GetDecimalFromLiteral((Literal)row.FindControl("litTravel"));
                }
                if (row.FindControl("litMileage") != null)
                {
                    mileage += GetDecimalFromLiteral((Literal)row.FindControl("litMileage"));
                }
                if (row.FindControl("litOther") != null)
                {
                    other += GetDecimalFromLiteral((Literal)row.FindControl("litOther"));
                }
            }

            foreach (GridViewRow row in grvParts.Rows)
            {
                if (row.FindControl("litPartTotal") != null)
                {
                    parts += GetDecimalFromLiteral((Literal)row.FindControl("litPartTotal"));
                }
            }

            litTotalLabor.Text = labor.ToString();
            litTotalTravel.Text = travel.ToString();
            litTotalMileage.Text = mileage.ToString();
            litTotalOther.Text = other.ToString();
            litTotalParts.Text = parts.ToString();
        }

        private decimal GetDecimalFromLiteral(Literal l)
        {
            return GetDecimalFromString(l.Text);
        }

        private decimal GetDecimalFromTextBox(TextBox t)
        {
            return GetDecimalFromString(t.Text);
        }

        private decimal GetDecimalFromString(string s)
        {
            s = s.Replace("$", "").Replace(",", "");

            decimal result = 0.00M;

            if (Decimal.TryParse(s, out result))
            {
                return result;
            }
            else
            {
                return 0;
            }
        }
    }
}